package org.web.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.web.ssm.dto.ResultModel;
import org.web.ssm.model.Product;
import org.web.ssm.model.queryVo.BaseQuery;
import org.web.ssm.service.IProductService;
import org.web.ssm.utils.DateUtils;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by hubin on 2016/6/15.
 */
@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private IProductService iProuctService;

    @RequestMapping(value = "/addproduct", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel<Product> addProduct(String title, Double rate, String createDate, String introduce) {
        ResultModel<Product> resultModel = new ResultModel<Product>();
        Product product = new Product();
        try {
            product.setCreateDate(DateUtils.stringToDate(createDate));
            product.setTitle(title);
            product.setIntroduce(introduce);
            product.setRate(rate);
            iProuctService.addProduct(product);

            resultModel.setMsg("添加成功！");
            resultModel.setSuccess(true);
            resultModel.setModel(product);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setMsg("添加失败！");
            resultModel.setSuccess(false);
        }
        return resultModel;
    }

    @RequestMapping(value = "/updaeproduct", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel<Product> updateProduct(String title, Double rate, String createDate, String introduce, int ProductID) {
        ResultModel<Product> resultModel = new ResultModel<Product>();
        Product product = new Product();
        try {
            product.setCreateDate(new Date());
            product.setTitle(title);
            product.setIntroduce(introduce);
            product.setRate(rate);
            product.setId(ProductID);
            iProuctService.updateProduct(product);

            resultModel.setMsg("修改成功！");
            resultModel.setSuccess(true);
            resultModel.setModel(product);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel.setMsg("修改失败！");
            resultModel.setSuccess(false);
        }
        return resultModel;
    }

    @RequestMapping("/delproduct")
    @ResponseBody
    public ResultModel<Product> delProduct(int ProductID) {
        ResultModel<Product> result = new ResultModel<Product>();
        try {
            iProuctService.delProduct(ProductID);
            result.setMsg("删除成功！");
            result.setSuccess(true);
        } catch (Exception e) {
            result.setMsg("删除失败！");
            result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping("/tookproduct")
    @ResponseBody
    public ResultModel<Product> tookProduct(int ProductID) {
        ResultModel<Product> result = new ResultModel<Product>();
        try {
            iProuctService.tookProduct(ProductID);
            result.setMsg("设置成功！");
            result.setSuccess(true);
        } catch (Exception e) {
            result.setMsg("设置失败！");
            result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping(value = "/getproductlist", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultModel<BaseQuery<Product>> getProductList(HttpSession httpSession, int pageSize, int pageCurrent, String keyword) {
        ResultModel<BaseQuery<Product>> product = new ResultModel<BaseQuery<Product>>();
        try {
            BaseQuery<Product> productQuery = iProuctService.getProductList(1, pageSize, pageCurrent, keyword);
            product.setModel(productQuery);
            product.setSuccess(true);
            product.setMsg("查询成功");
        } catch (Exception e) {
            product.setModel(null);
            product.setSuccess(false);
            product.setMsg("查询失败");
        }
        return product;
    }
}
